{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8888028a",
   "metadata": {},
   "source": [
    "# 习题3\n",
    "\n",
    "sympy.rref()求矩阵的最简形式，并显示最大无关组所在列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39214fe1",
   "metadata": {},
   "source": [
    "**<font size=5>1.</font>**设$3(\\alpha_1-\\alpha)+2(\\alpha_2+\\alpha)=5(\\alpha_3+\\alpha)$其中$\\alpha=(2,5,1,3)^T,\\alpha_2=(1,0,1,5)^T,\\alpha_3=(4,1,-1,1)^T$,求$\\alpha$.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3dc68f73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2\\\\- \\frac{5}{3}\\\\- \\frac{5}{3}\\\\- \\frac{7}{3}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[   2],\n",
       "[-5/3],\n",
       "[-5/3],\n",
       "[-7/3]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "alpha1= np.array([[2],[5],[1],[3]])\n",
    "alpha2 = np.array([[1],[0],[1],[5]])\n",
    "alpha3=np.array([[4],[1],[-1],[1]])\n",
    "\n",
    "alpha1=sp.Matrix(alpha1)\n",
    "alpha2=sp.Matrix(alpha2)\n",
    "alpha3=sp.Matrix(alpha3)\n",
    "\n",
    "(5*alpha3-2*alpha2-3*alpha1)/6\n",
    "# 3*A*B-2*A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf805c56",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b4dbce03",
   "metadata": {},
   "source": [
    "**<font size=5>2.</font>**求向量$\\alpha_1=\\begin{pmatrix}1\\\\ 0 \\\\-1\\end{pmatrix}$,$\\alpha_2=\\begin{pmatrix}2\\\\  1 \\\\3\\end{pmatrix}$,$\\alpha_3=\\begin{pmatrix}7\\\\ 3 \\\\4\\end{pmatrix}$的线性组合$3\\alpha_1+2\\alpha_2-4\\alpha_3$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "03703e6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-21\\\\-10\\\\-13\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-21],\n",
       "[-10],\n",
       "[-13]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "alpha1= np.array([[1],[0],[-1]])\n",
    "alpha2 = np.array([[2],[1],[3]])\n",
    "alpha3=np.array([[7],[3],[4]])\n",
    "\n",
    "alpha1=sp.Matrix(alpha1)\n",
    "alpha2=sp.Matrix(alpha2)\n",
    "alpha3=sp.Matrix(alpha3)\n",
    "\n",
    "3*alpha1+2*alpha2-4*alpha3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dee53b16",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c4b2bf2a",
   "metadata": {},
   "source": [
    "**<font size=5>3.</font>**试问向量$\\beta$能否由其余向量线性表示？若能，写出线性表示式.\n",
    "\n",
    "$\\alpha_1=(3,-2,2)^T$，$\\alpha_2=(-2,1,2)^T$，$\\alpha_3=(1,2,-1)^T$，$\\beta=(4,5,6)^T$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "48409c43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\left( \\frac{5}{2}, \\  \\frac{12}{5}, \\  \\frac{19}{5}\\right)\\right\\}$"
      ],
      "text/plain": [
       "{(5/2, 12/5, 19/5)}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "x1,x2,x3 = sp.symbols(\"x1 x2 x3\")\n",
    "A = sp.Matrix([[2,-2,1],[-2,1,2],[2,2,-1]])\n",
    "b = sp.Matrix([[4],[5],[6]])\n",
    "b0 = sp.Matrix([[0],[0],[0]])\n",
    "\n",
    "system1 = A,b\n",
    "\n",
    "result = sp.linsolve(system1,x1,x2,x3)\n",
    "result\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5aae631",
   "metadata": {},
   "source": [
    "所以$\\beta=\\frac{5}{2}\\alpha_1+\\frac{12}{5}\\alpha_2+\\frac{19}{5}\\alpha_3$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e826406e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5b46bd59",
   "metadata": {},
   "source": [
    "**<font size=5>4.</font>**已知向量组$B:\\beta_1,\\beta_2,\\beta_3$由向量组$A:\\alpha_1,\\alpha_2,\\alpha_3$的线性表示为$\\beta_1=\\alpha_1-\\alpha_2+\\alpha_3$，$\\beta_2=\\alpha_1+\\alpha_2-\\alpha_3$，$\\beta_3=-\\alpha_1+\\alpha_2+\\alpha_3$，试将向量组$(A)$的向量由向量组$(B)$的向量线性表示."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8c0c064e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{2} & \\frac{1}{2} & 0\\\\0 & \\frac{1}{2} & \\frac{1}{2}\\\\\\frac{1}{2} & 0 & \\frac{1}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1/2, 1/2,   0],\n",
       "[  0, 1/2, 1/2],\n",
       "[1/2,   0, 1/2]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "A = sp.Matrix([[1,-1,1],[1,1,-1],[-1,1,1]])\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad63d656",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "68e9d8e8",
   "metadata": {},
   "source": [
    "**<font size=5>5.</font>**证明：向量$\\beta=(-1,1,5)$是向量$\\alpha_1=(1,2,3)^T$,$\\alpha_2=(0,1,4)^T$,$\\alpha_3=(2,3,6)^T$的线性组合，并具体将$\\beta$用$\\alpha_1,\\alpha_2,\\alpha_3$表示出来."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "19e243b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "化简：\n",
      "(Matrix([\n",
      "[1, 0, 0],\n",
      "[0, 1, 0],\n",
      "[0, 0, 1]]), (0, 1, 2))\n",
      "线性表示：\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left\\{\\left( 1, \\  2, \\  -1\\right)\\right\\}$"
      ],
      "text/plain": [
       "{(1, 2, -1)}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,0,2],[2,1,3],[3,4,6]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "print('化简：')\n",
    "print(A.rref())\n",
    "\n",
    "x1,x2,x3 = sp.symbols(\"x1 x2 x3\")\n",
    "b = sp.Matrix([[-1],[1],[5]])\n",
    "system1 = A,b\n",
    "\n",
    "result = sp.linsolve(system1,x1,x2,x3)\n",
    "\n",
    "print('线性表示：')\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f6eff57",
   "metadata": {},
   "source": [
    "所以$\\beta=\\alpha_1+2\\alpha_2-\\alpha_3$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34ece3e4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6c3d8df5",
   "metadata": {},
   "source": [
    "**<font size=5>6.</font>**已知$\\alpha_1,\\alpha_2,\\alpha_3$线性无关，证明$\\alpha_1+\\alpha_2$，$3\\alpha_2+2\\alpha_3$，$\\alpha_1-2\\alpha_2+\\alpha_3$线性无关."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f8c3e460",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{7}{9} & - \\frac{1}{9} & \\frac{2}{9}\\\\\\frac{2}{9} & \\frac{1}{9} & - \\frac{2}{9}\\\\- \\frac{1}{3} & \\frac{1}{3} & \\frac{1}{3}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 7/9, -1/9,  2/9],\n",
       "[ 2/9,  1/9, -2/9],\n",
       "[-1/3,  1/3,  1/3]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,1,0],[0,3,2],[1,-2,1]])\n",
    "A=sp.Matrix(A)\n",
    "A.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "887ed0a2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d2c2f3fb",
   "metadata": {},
   "source": [
    "**<font size=5>7.</font>**已知$\\beta_1=\\alpha_1+\\alpha_2$，$\\beta_2=\\alpha_1-\\alpha_2$，$\\beta_3=3\\alpha_1-2\\alpha_2$，证明$\\beta_1,\\beta_2,\\beta_3$是线性相关的."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4203b923",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Matrix([\n",
       " [1, 0],\n",
       " [0, 1],\n",
       " [0, 0]]),\n",
       " (0, 1))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,1],[1,-1],[3,-2]])\n",
    "A=sp.Matrix(A)\n",
    "A.rref()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10342fb9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "87abffe7",
   "metadata": {},
   "source": [
    "**<font size=5>8.</font>**判断向量组$\\alpha_1=(1,2,0,1)^T,\\alpha_2=(1,3,0,-1)^T,\\alpha_3=(-1,-1,1,0)^T$是否线性相关"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ac9e0ed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Matrix([\n",
       " [1, 0, 0,  5],\n",
       " [0, 1, 0, -2],\n",
       " [0, 0, 1,  3]]),\n",
       " (0, 1, 2))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,2,0,1],[1,3,0,-1],[-1,-1,1,0]])\n",
    "A=sp.Matrix(A)\n",
    "A.rref()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95b9159d",
   "metadata": {},
   "source": [
    "所以，$\\alpha_1,\\alpha_2,\\alpha_3$线性无关"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "808b4095",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "077ae84f",
   "metadata": {},
   "source": [
    "**<font size=5>9.</font>**求下列向量组的秩，并求一个最大无关组.\n",
    "\n",
    "(1)$\\alpha_1=(2,4,2)^T,\\alpha_2=(1,1,0)^T,\\alpha_3=(2,3,1)^T,\\alpha_4=(3,5,2)^T;$\n",
    "\n",
    "(2)$\\alpha_1=\\begin{pmatrix}1\\\\ 2 \\\\-1 \\\\ 4\\end{pmatrix}$,$\\alpha_2=\\begin{pmatrix}9\\\\  100 \\\\ 10 \\\\4\\end{pmatrix}$,$\\alpha_3=\\begin{pmatrix}7\\\\ 3 \\\\4\\end{pmatrix}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "95067429",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Matrix([\n",
       " [1, 0, 1/2, 1],\n",
       " [0, 1,   1, 1],\n",
       " [0, 0,   0, 0]]),\n",
       " (0, 1))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#（1）\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[2,1,2,3],[4,1,3,5],[2,0,1,2]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.rref()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f07d26d",
   "metadata": {},
   "source": [
    "所以$\\alpha_1,\\alpha_2$是向量组的一个最大无关组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4e5d01fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Matrix([\n",
       " [1, 0, -2],\n",
       " [0, 1,  0],\n",
       " [0, 0,  0],\n",
       " [0, 0,  0]]),\n",
       " (0, 1))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#（2）\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,9,-2],[2,100,-4],[-1,10,2],[4,4,-8]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.rref()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f02d48a",
   "metadata": {},
   "source": [
    "所以，$\\alpha_1,\\alpha_2$是向量组的一个最大无关组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9feba5dc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "866c1cbe",
   "metadata": {},
   "source": [
    "**<font size=5>10.</font>**求向量组$\\alpha_1=\\begin{pmatrix}1 \\\\ 1 \\\\2 \\\\ 2\\end{pmatrix}$，$\\alpha_2=\\begin{pmatrix}1 \\\\ 2 \\\\1 \\\\ 3\\end{pmatrix}$，$\\alpha_3=\\begin{pmatrix}1 \\\\ -1 \\\\ 4 \\\\ 0 \\end{pmatrix}$，$\\alpha_4=\\begin{pmatrix} 1 \\\\ 0 \\\\ 3 \\\\ 1 \\end{pmatrix}$的一个最大无关组，并且其余向量用该最大无关组线性表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "de43b7b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Matrix([\n",
       " [1, 0,  3,  2],\n",
       " [0, 1, -2, -1],\n",
       " [0, 0,  0,  0],\n",
       " [0, 0,  0,  0]]),\n",
       " (0, 1))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,1,1,1],[1,2,-1,0],[2,1,4,3],[2,3,0,1]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "A.rref()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6d6234d",
   "metadata": {},
   "source": [
    "所以$\\alpha_1,\\alpha_2$是向量组的一个最大无关组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cab26dce",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e29307b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "23ae7051",
   "metadata": {},
   "source": [
    "**<font size=5>12.</font>**已知$(\\alpha_1,\\alpha_2)=\\begin{pmatrix}2 & 3\\\\ 0 & -2 \\\\ -1 & 1 \\\\ 3 & -1\\end{pmatrix}$，$(\\beta_1,\\beta_2)=\\begin{pmatrix} -5 & 4\\\\ 6 & -4 \\\\ -5 & 3 \\\\ 9 & -5\\end{pmatrix}$，证明向量组$(\\alpha_1,\\alpha_2)$与向量组$(\\beta_1,\\beta_2)$等价。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6a2d8458",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Matrix([\n",
      "[1, 0],\n",
      "[0, 1],\n",
      "[0, 0],\n",
      "[0, 0]]), (0, 1))\n",
      "(Matrix([\n",
      "[1, 0],\n",
      "[0, 1],\n",
      "[0, 0],\n",
      "[0, 0]]), (0, 1))\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[2,3],[0,-2],[-1,1],[3,-1]])\n",
    "A=sp.Matrix(A)\n",
    "B=np.array([[-5,4],[6,-4],[-5,3],[9,-5]])\n",
    "B=sp.Matrix(B)\n",
    "\n",
    "print(A.rref())\n",
    "print(B.rref())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1b30a44",
   "metadata": {},
   "source": [
    "两个向量组最大无关组都是两个，且相等，因此等价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4232296",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "103f1554",
   "metadata": {},
   "source": [
    "**<font size=5>13.</font>**设$\\alpha_1=(6,a+1,3)^T$，$\\alpha_2=(a,2,-2)^T$，$\\alpha_3=(a,1,0)^T$，试问：\n",
    "\n",
    "（1）$a$为何值时，$\\alpha_1$，$\\alpha_2$线性相关？线性无关？\n",
    "\n",
    "（2）$a$为何值时，$\\alpha_1$，$\\alpha_2$，$\\alpha_3$线性相关？线性无关？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72d3ff03",
   "metadata": {},
   "source": [
    "（1）\n",
    "\n",
    "$\\alpha_1$，$\\alpha_2$线性相关时，$\\alpha_1=k\\alpha_2$,可得方程组\n",
    "\n",
    "$\\begin{equation*}\n",
    "     \\begin{cases}\n",
    "         6=ka \\\\\n",
    "         a+1=2k \\\\ \n",
    "         3=-2k\n",
    "     \\end{cases}\n",
    " \\end{equation*}$=>$\n",
    " \\begin{equation*}\n",
    "     \\begin{cases}\n",
    "         k=-\\frac{3}{2} \\\\\n",
    "         a=-4\n",
    "     \\end{cases}\n",
    " \\end{equation*}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6d8f6eb",
   "metadata": {},
   "source": [
    "因此，当$a=-4$时，$\\alpha_1$与$\\alpha_2$线性相关，$a\\neq-4$时，$\\alpha_1$与$\\alpha_2$线性无关。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0d245ede",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-4, 3/2]\n"
     ]
    }
   ],
   "source": [
    "#（2）\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([[6,'a','a'],['a+1',2,1],[3,-2,0]])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.Symbol('a')\n",
    "print(sp.solve(M.det(),x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a943ec8",
   "metadata": {},
   "source": [
    "因此，当$a=-4或\\frac{3}{2}$时，$\\alpha_1$，$\\alpha_2$与$\\alpha_3$线性相关，$a\\neq-4,\\frac{3}{2}$时，$\\alpha_1$，$\\alpha_2$与$\\alpha_3$线性无关。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bacf2eb9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "0d3f7183",
   "metadata": {},
   "source": [
    "**<font size=5>14.</font>**已知向量组$\\alpha_1=(1,1,2)^T$，$\\alpha_2=(3,t,1)^T$，$\\alpha_3=(0,2,-t)$线性相关，求$t$的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f4836fbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2, 5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([[1,1,2],[3,'t',1],[0,2,'-t']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.Symbol('t')\n",
    "print(sp.solve(M.det(),x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cfbb546",
   "metadata": {},
   "source": [
    "所以，当$t=-2或5$时，$\\alpha_1$，$\\alpha_2$与$\\alpha_3$线性相关。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c6ed5e1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "96c0ab09",
   "metadata": {},
   "source": [
    "**<font size=5>15.</font>**给定向量$\\alpha_1=(-2,4,1)^T$，$\\alpha_2=(-1,3,5)^T$，$\\alpha_3=(2,-3,1)$，$\\beta=(1,1,3)^T$，试证明：向量组$\\alpha_1$，$\\alpha_2$，$\\alpha_3$是三维向量空间$R^3$的一个基，并将向量$\\beta$用这个基线性表示."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2f4e93d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Matrix([\n",
      "[1, 0, 0],\n",
      "[0, 1, 0],\n",
      "[0, 0, 1]]), (0, 1, 2))\n",
      "{(4, -1, 4)}\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[-2,-1,2],[4,3,-3],[1,5,1]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "print(A.rref())\n",
    "\n",
    "x, y, z = symbols(\"x y z\")\n",
    "eq = Matrix(([-2,-1,2,1],[4,3,-3,1],[1,5,1,3]))\n",
    "result = sp.linsolve(eq, [x, y, z])\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bb7ed69",
   "metadata": {},
   "source": [
    "因此，$\\beta=4\\alpha_1-\\alpha_2+4\\alpha_3$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70c8f883",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "8b111c1f",
   "metadata": {},
   "source": [
    "**<font size=5>16.</font>**设向量组$A：\\alpha_1=(1,0,1)^T，\\alpha_2=(2,2,0)^T，\\alpha_3=(2,4,-1)^T$，向量组$B:\\beta_1=(-1,2,4)^T$，$\\beta_2=(2,4,-4)^T$.试证明：向量组A是三维空间$R^3$的一个基，并将向量组$B$用这个基线性表示."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "50522327",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Matrix([\n",
      "[1, 0, 0],\n",
      "[0, 1, 0],\n",
      "[0, 0, 1]]), (0, 1, 2))\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}11 & -6\\\\-13 & 6\\\\7 & -2\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 11, -6],\n",
       "[-13,  6],\n",
       "[  7, -2]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,2,2],[0,2,4],[1,0,-1]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "print(A.rref())\n",
    "\n",
    "B=np.array([[-1,2],[2,4],[4,-4]])\n",
    "X = A.inv()*B\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f9d382c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48dabb69",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e3396bca",
   "metadata": {},
   "source": [
    "**<font size=5>17.</font>**试证：由$\\alpha_1=(0,1,1)^T$，$\\alpha_2=(1,0,1)^T$，$\\alpha_3=(1,1,0)^T$所生成的向量空间就是$R^3$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8a039012",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Matrix([\n",
      "[1, 0, 0],\n",
      "[0, 1, 0],\n",
      "[0, 0, 1]]), (0, 1, 2))\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[0,1,1],[1,0,1],[1,1,0]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "print(A.rref())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "923ce46f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9a530c32",
   "metadata": {},
   "source": [
    "**<font size=5>18.</font>**已知$R^3$的两个基为$\\alpha_1=\\begin{pmatrix}1 \\\\ 1 \\\\ 1 \\end{pmatrix}$，$\\alpha_2=\\begin{pmatrix}1 \\\\ 0 \\\\ -1 \\end{pmatrix}$，$\\alpha_3=\\begin{pmatrix}1 \\\\ 0 \\\\ 1 \\end{pmatrix}$，$\\beta_1=\\begin{pmatrix}1 \\\\ 2 \\\\ 1 \\end{pmatrix}$,$\\beta_2=\\begin{pmatrix}2 \\\\ 3 \\\\ 4 \\end{pmatrix}$,$\\beta_3=\\begin{pmatrix}3 \\\\ 4 \\\\ 3 \\end{pmatrix}$，求由基$\\alpha_1$，$\\alpha_2$，$\\alpha_3$到基$\\beta_1$，$\\beta_2$，$\\beta_3$的过渡矩阵$P$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a9e40bcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}2 & 3 & 4\\\\0 & -1 & 0\\\\-1 & 0 & -1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 2,  3,  4],\n",
       "[ 0, -1,  0],\n",
       "[-1,  0, -1]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "A=np.array([[1,1,1],[1,0,0],[1,-1,1]])\n",
    "A=sp.Matrix(A)\n",
    "\n",
    "B=np.array([[1,2,3],[2,3,4],[1,4,3]])\n",
    "B=sp.Matrix(B)\n",
    "\n",
    "P=A.inv()*B\n",
    "P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d284e0a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
